const mysql = require('mysql')
const moment = require('moment')
// 加密模块中的随机生成数
const { randomInt } = require('crypto')
const sqlconfig = require('./config')
const { resolve } = require('path')

// 查询单条
const searchOne = (tables, field, str) => {
  const selectSql = `select * from ${tables} where ${field}=?`;
  const params = [str]
  return new Promise((resolve, reject) => {
    sqlconfig.query(selectSql, params, (err, results) => {
      // 查询数据失败
      if (err) {
     
        reject(err)
        return
      }
      resolve(results)
      return
    })
  })

}
// 插入单条
const insertOne = (tables = 'users', field = 'username,password', ...str) => {
  const requestStr = Array(str.length).fill("?").join(",")
  const sqlStr = `insert into ${tables}(${field}) values(${requestStr})`
  return new Promise((resolve, reject) => {
    sqlconfig.query(sqlStr, [...str], (err, result) => {
      if (err) {
        reject(err)
        return
      };

      if (result.affectedRows === 1) {
        resolve(result)
        return
      }
    })
  })

}
module.exports = {
  searchOne,
  insertOne
}